package com.syni.mdd.yhd.common.all.quartz.groupBuy.job;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.BmsDxGroupBuy;
import com.syni.mdd.yhd.common.all.entity.BmsDxOrder;
import com.syni.mdd.yhd.common.all.service.BmsDxGroupBuyService;
import com.syni.mdd.yhd.common.all.service.BmsDxOrderService;
import com.syni.mdd.sjb.common.component.utils.common.utils.DateTimeUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.SpringUtils;
import com.syni.mdd.yhd.common.all.service.groupBuy.BmsDxGroupBuyRecordService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.Date;
import java.util.Map;

/**
 * @className GroupMessageQuartz
 * @description TOO
 * @Author cfx
 * @DATE 2019/7/8 9:37
 * @VERSION 1.0
 **/
public class LimitGroupBuyTimingJob implements Job {

    //上架限时团购
    public static final int HANDLE_ONE = 1;
    //下架限时团购
    public static final int HANDLE_TWO = 2;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Date nowTime = new Date();
        BmsDxGroupBuyService bmsDxGroupBuyService = SpringUtils.getBean(BmsDxGroupBuyService.class);
        BmsDxGroupBuyRecordService bmsDxGroupBuyRecordService = SpringUtils.getBean(BmsDxGroupBuyRecordService.class);
        Map<String,Object> paramsMap = jobExecutionContext.getJobDetail().getJobDataMap();
        BmsDxGroupBuy bmsDxGroupBuy = (BmsDxGroupBuy) paramsMap.get("bmsDxGroupBuy");
        Integer handle = Integer.parseInt(paramsMap.get("handle").toString());
        if (bmsDxGroupBuy.getIsTimeLimit() != null && bmsDxGroupBuy.getIsTimeLimit() == Constants.COMMON_FLAG_TRUE_INT) {
            if (handle == HANDLE_ONE) {
                if(bmsDxGroupBuy.getStartSellTime() != null){
                    if (bmsDxGroupBuy.getStatus() == Constants.NOT_ONLINE_STATUS) {
                        if (DateTimeUtils.converDateToInterger(nowTime) >= DateTimeUtils.converDateToInterger(bmsDxGroupBuy.getStartSellTime())) {
                            bmsDxGroupBuy.setStatus(Constants.ONLINE_STATUS);
                            bmsDxGroupBuy.setOnlineTime(nowTime);
                        }
                    }
                }
            }else if(handle == HANDLE_TWO){
                if (bmsDxGroupBuy.getStatus() == Constants.ONLINE_STATUS) {
                    if(bmsDxGroupBuy.getLimitBuyTime() != null){
                        if (DateTimeUtils.converDateToInterger(nowTime) >= DateTimeUtils.converDateToInterger(bmsDxGroupBuy.getLimitBuyTime())) {
                            bmsDxGroupBuy.setStatus(Constants.ROLLED_ONLINE_STATUS);
                            bmsDxGroupBuy.setOfflineTime(nowTime);
                            bmsDxGroupBuyRecordService.addGroupBuyRecord(bmsDxGroupBuy, BmsDxGroupBuyRecordService.RECORD_OFFLINE);
                        }
                    }
                }
            }
        }
        bmsDxGroupBuyService.getBaseDao().save(bmsDxGroupBuy);
    }
}
